﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.HomeServer.Controls;
using Umbrella.Net.Contracts;
using Umbrella.Whs.SharePoint;
using Umbrella.Whs.Contracts;
using Umbrella.Whs.WhsAddin.Controls;

namespace Umbrella.Whs.WhsAddin.Wizards.SharePointInstallation
{
    internal partial class Step10_Prepare : WizardPageClientBannerTop
    {
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(Step10_Prepare));
        private SingletonServiceFactory _service = new SingletonServiceFactory();

        private SharePointInstallationWizard ParentWizard2
        {
            get { return (SharePointInstallationWizard)this.ParentWizard; }
        }

        public Step10_Prepare()
        {
            InitializeComponent();
        }

        private void Step10_Prepare_Load(object sender, EventArgs e)
        {
            this.filledBar.Value1 = 0;
            this.statusLabel.Text = Resources.txtPreparing;
        }

        public override void StartRealWork()
        {
            base.StartRealWork();

            this.loadBgWorker.RunWorkerAsync();
        }

        private void loadBgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                _log.Error("Failed to prepare installation", e.Error);

                this.filledBar.Value1 = 0;
                this.statusLabel.Text = Resources.txtFailed;

                QMessageBox.Show(
                    e.Error.Message,
                    Resources.txtInstallFailedTitle,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }

            if ((e.Error != null) || e.Cancelled)
            {
                QMessageBox.Show(
                    Resources.txtInstallAbortedText,
                    Resources.txtInstallAbortedTitle,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning
                    );

                this.ParentWizard2.Close();

                return;
            }

            // goto next page
            SetCurrentPage<Step20_SelectLanguagePacks>();
        }

        private void loadBgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (this.loadBgWorker.CancellationPending)
                    return;

	            // check for updates
	            {
	                this.loadBgWorker.ReportProgress(5, Resources.txtCheckingForUpdates);
	
	                var ri = _service.Channel.CheckNewerVersionNow();
	
	                //if (ri.HasValue) ;
	            }

                if (this.loadBgWorker.CancellationPending)
                    return;

                // gather installation files
	            var ipm = new InstallationPackagesManager();
	
	            {
	                this.loadBgWorker.ReportProgress(20, Resources.txtGatherInstallationPackages);
	
	                var instFiles = ipm.GetInstallationFileList();
	
	                this.ParentWizard2.InstallationFiles = new List<SharePointInstallationFile>(instFiles);
	            }

                if (this.loadBgWorker.CancellationPending)
                    return;

                // gather languages
	            {
	                this.loadBgWorker.ReportProgress(50, Resources.txtGatherLanguages);
	
	                var instFiles = ipm.GetLanguageList();
	
	                this.ParentWizard2.Languages = new List<SharePointLanguage>(instFiles);
	            }

                if (this.loadBgWorker.CancellationPending)
                    return;

                // cleanup temporary files
                var im = new InstallationManager();

	            {
                    this.loadBgWorker.ReportProgress(70, Resources.txtCleaningUpTemporaryFiles);

                    // cleanup
	                im.CleanupInstallationTempDir();

                    // create temporary directory
                    var tempDir = this.ParentWizard2.InstallationTempDir;

                    tempDir.Create();
                }

                if (this.loadBgWorker.CancellationPending)
                    return;

                // done
	            this.loadBgWorker.ReportProgress(100, Resources.txtDone);
            }
            catch (Exception ex)
            {
                _log.Error("Failed to prepare SharePoint installation wizard", ex);

                throw;
            }
        }

        private void loadBgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.statusLabel.Text = (string)e.UserState;
            this.filledBar.Value1 = e.ProgressPercentage;
        }

        private void Step10_Prepare_CancelButtonClick(object sender, EventArgs e)
        {
            if (QMessageBox.Show(
                Resources.txtCancelVerifyText,
                Resources.txtCancelVerifyTitle,
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question
                ) != DialogResult.Yes)
            {
                return;
            }

            this.CancelButtonEnabled = false;

            this.loadBgWorker.CancelAsync();
        }

        private void filledBar_Click(object sender, EventArgs e)
        {

        }
    }
}
